##############################################################
#Figure C5: Distribution of Risk Aversion and Race in CCES 2014
##############################################################

data <- read.dta("./input/cces-subset.dta")

smalld <- data[,c("employed", "out_of_labour", "race_14", "risk", "dem14", "education", 
                  "female", "income", "income_quarters", "age")]

smalld$income <- as.factor(smalld$income)
smalld$income_quarters <- as.factor(smalld$income_quarters)
smalld$education <- as.factor(smalld$education)

data4 <- na.omit(smalld) ##taking out missing values 

data4.2 <- data4 %>% 
  group_by(race_14,risk) %>% 
  summarise(count=n()) %>% 
  mutate(perc=count/sum(count))

ggplot(data4.2, aes(x=as.factor(risk), y=perc, group=as.factor(race_14), fill=as.factor(race_14))) +
  geom_bar(stat= "identity", position="dodge") + 
  scale_y_continuous(labels=percent) + 
  facet_grid(~race_14) +   
  ylab("Percentage") + 
  xlab ("Risk Aversion")  + 
  theme_bw() + 
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor =   element_blank(),
        legend.position = "none") + 
  scale_x_discrete(breaks=c(0,1,2,3),labels= c("Taker", "", "", "Averse"))  +
  scale_fill_manual(values=c("grey60", "grey60", "grey60", "grey60", "grey60", "grey60" ,"grey60"))

ggsave("./figures/figc5.pdf")  